package org.opens.web.db.controller;

import org.opens.db.column.entity.TableEntity;
import org.opens.util.R;
import org.opens.web.db.entity.dto.JdbcConnectionDTO;
import org.opens.web.db.service.DbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping(value = "/db")
public class DbController {

    @Autowired
    private DbService dbService;

    /**
     * 测试数据库连接信息是否可用
     * @param jdbcConnectionDTO 包含数据库连接信息
     */
    @PostMapping(value = "/validJdbcConnection")
    public R<Boolean> validJdbcConnection(@RequestBody JdbcConnectionDTO jdbcConnectionDTO) {
        return R.success(dbService.validJdbcConnection(jdbcConnectionDTO));
    }

    /**
     * 根据数据库连接信息获取所有的表
     * @param jdbcConnectionDTO 包含数据库连接信息
     * @return  查询到的所有的表
     */
    @PostMapping(value = "/getTableInfo")
    public R<List<TableEntity>> getTableInfo(@RequestBody JdbcConnectionDTO jdbcConnectionDTO) {
        return R.success(dbService.getTableInfo(jdbcConnectionDTO));
    }

}
